package com.java2007.hotel.dao.impl;

import com.java2007.hotel.dao.FoodTypeDao;
import com.java2007.hotel.pojo.FoodType;
import com.java2007.hotel.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author Reckeys
 * @version V1.0
 * @date 2021/01/22
 **/
public class FoodTypeDaoImpl implements FoodTypeDao {
    /**
     * 通过TypeName模糊查询
     * @return
     */
    @Override
    public List<FoodType> finfByTypeName(String typeName) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT type_id typeId,type_name typeName,type_create_time typeCreateTime,type_update_time typeUpdateTime,type_create_user_id typeCreateUserId FROM t_food_type WHERE type_name LIKE ?";
        return queryRunner.query(sql,new BeanListHandler<>(FoodType.class),typeName);

    }

    /**
     * 通过id删除
     * @param typeId
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteById(Integer typeId) throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        String sql = "DELETE FROM t_food_type WHERE type_id = ?";
        return queryRunner.update(JdbcUtils.getConnection(),sql,typeId);

    }

    /**
     * 通过id查询信息
     * @param typeId
     * @return
     */
    @Override
    public FoodType findByTypeId(Integer typeId) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT type_id typeId,type_name typeName,type_create_time typeCreateTime,type_update_time typeUpdateTime,type_create_user_id typeCreateUserId FROM t_food_type WHERE type_id = ?";
        return queryRunner.query(sql,new BeanHandler<>(FoodType.class),typeId);
    }

    /**
     * 更新信息
     * @param foodType
     * @return
     */
    @Override
    public int updateFoodType(FoodType foodType) throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        String sql = "UPDATE t_food_type SET type_name = ?,type_update_time = ?,type_create_user_id = ? WHERE type_id = ?";
        return queryRunner.update(JdbcUtils.getConnection(),sql,foodType.getTypeName(),foodType.getTypeUpdateTime(),foodType.getTypeCreateUserId(),foodType.getTypeId());
    }

    /**
     * 添加菜类别信息
     * @param foodType
     * @return
     */
    @Override
    public int addFoodType(FoodType foodType) throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        String sql = "INSERT INTO t_food_type (type_name,type_create_time,type_create_user_id) VALUES (?,?,?)";
        return queryRunner.update(JdbcUtils.getConnection(),sql,foodType.getTypeName(),foodType.getTypeUpdateTime(),foodType.getTypeCreateUserId());
    }
}
